package rs.belex.belexdroid.services.impl.ksoap2;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import rs.belex.belexdroid.beans.ShareBean;
import rs.belex.belexdroid.beans.ShareChangeBean;
import rs.belex.belexdroid.beans.ShareHistoryBean;
import rs.belex.belexdroid.beans.interfaces.Share;
import rs.belex.belexdroid.beans.interfaces.ShareChange;
import rs.belex.belexdroid.beans.interfaces.ShareHistory;
import rs.belex.belexdroid.services.interfaces.IShareProvider;
import android.util.Log;

public class ShareProviderKsoap2Impl extends ServiceClientKsoap2Impl implements
		IShareProvider {
	private static String TAG = ShareProviderKsoap2Impl.class.getSimpleName();

	public ShareProviderKsoap2Impl() {
	};

	public Share getShare(String symbol) {

		Log.d(TAG, "Get Share called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getShare";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("symbol", symbol);

		SoapObject response = super.requestComplexType(request, SOAP_ACTION);

		Share share = new ShareBean();
		share.setName(response.getProperty("name").toString());
		share.setType(response.getProperty("type").toString());
		share.setSymbol(response.getProperty("symbol").toString());
		share.setValue(new Double(response.getProperty("value").toString()));

		Log.d(TAG, "Get Share successful");

		return share;
	}

	public List<Share> getAll() {

		List<Share> shares = new LinkedList<Share>();
		Log.d(TAG, "Get all Shares called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getAllShares";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		List<SoapObject> response = super.requestComplexTypeList(request,
				SOAP_ACTION);

		// Iterate through response and make a List of shares
		for (SoapObject s : response) {
			Share share = new ShareBean();
			share.setName(s.getProperty("name").toString());
			share.setType(s.getProperty("type").toString());
			share.setSymbol(s.getProperty("symbol").toString());
			share.setValue(new Double(s.getProperty("value").toString()));
			shares.add(share);
		}

		Log.d(TAG, "Get All Shares successful");

		return shares;

	}

	public ShareHistory getHistory(Share share) {

		Log.d(TAG, "Get History called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getHistory";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("symbol", share.getSymbol());

		SoapObject response = super.requestComplexType(request, SOAP_ACTION);

		SoapObject changesResponse = (SoapObject) response
				.getProperty("changes");

		int propsNum = changesResponse.getPropertyCount();
		List<SoapObject> changesSoap = new LinkedList<SoapObject>();
		// Iterate through properties and make the list
		for (int i = 0; i < propsNum; i++) {
			SoapObject element = (SoapObject) changesResponse.getProperty(i);
			changesSoap.add(element);
		}

		List<ShareChange> changes = new LinkedList<ShareChange>();
		// iterate through changesResponse and make list of changes
		for (SoapObject c : changesSoap) {
			ShareChange change = new ShareChangeBean();
			change.setChangeValue(Double.parseDouble(c.getProperty(
					"changeValue").toString()));

			Date date = dateParser(c.getProperty("time").toString());

			change.setTime(date);
			change.setTimestamp(date.getTime());
			changes.add(change);
		}

		SoapObject shareResponse = (SoapObject) response.getProperty("share");

		Share share2 = new ShareBean();
		share2.setName(shareResponse.getProperty("name").toString());
		share2.setType(shareResponse.getProperty("type").toString());
		share2.setSymbol(shareResponse.getProperty("symbol").toString());
		share2.setValue(new Double(shareResponse.getProperty("value")
				.toString()));

		Log.d(TAG, "Get History successful");

		ShareHistory history = new ShareHistoryBean();
		history.setShare(share2);
		history.setChanges(changes);
		return history;
	}

	public List<ShareChange> getChangesOnDate(Share share, Date date) {

		Log.d(TAG, "Get Changes called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getChangesOnDate";

		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		String day = formatter.format(date);
		
		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("symbol", share.getSymbol());
		request.addProperty("day", day);

		List<SoapObject> response = super.requestComplexTypeList(request, SOAP_ACTION);

		List<ShareChange> changes = new LinkedList<ShareChange>();
		// iterate through changesResponse and make list of changes
		for (SoapObject c : response) {
			ShareChange change = new ShareChangeBean();
			change.setChangeValue(Double.parseDouble(c.getProperty(
					"changeValue").toString()));

			Date changeDate = dateParser(c.getProperty("time").toString());

			change.setTime(changeDate);
			change.setTimestamp(changeDate.getTime());
			changes.add(change);
		}

		Log.d(TAG, "Get Changes successful");

		return changes;
	}
}
